arc-is
A simple, efficient but comprehensive typechecking suite
In addition to native typechecking, it provides reliable behavior for:
Install
$ npm install --save arc-is
Usage
var is = require('arc-is');
is(someUnknownVariable [,returnObjectType]);
Params
someUnknownVariable
is the variable you want to typecheck.returnObjectType
is optional and tells the function if you want it to return a complex type (non native) if available. If false, is() will always return a lowercase string, otherwise it will return a case sensitive string.
Examples
var simpleObj = {};
is(simpleObj);
is(simpleObj,true);
var es6global = new Map;
is(es6global);
is(es6global,true);
class MyClass{ toString(){ return '[object '+this.constructor.name+']'; } }
is(new MyClass);
is(new MyClass,true);
class MyArray extends Array{ return '[object '+this.constructor.name+']'; } }
is(new MyArray);
is(new MyArray,true);
Caveats
- It is far more efficient to not check for a complexType
- If a class does not overwrite the toString() method, it will return the parent's toString() which will likely be Object
- This goes for natively extended globally available classes such as the Error subclasses (ie. TypeError), as these do not overwrite the toString() method, they return the parent's toString() which is Error
- For is() to return the appropriate class name, toString must return the name in the format of '[object $NAME]' which is standard for all native classes